<template>
	<div>
		<navigation-bar>
			<div class="searchContent">
				<el-input v-model="name" placeholder="请输入名称" size="mini"
					style="width: 150px; margin-right: 10px"></el-input>
				<custom-button type="search" style="margin-right: 10px" @searchClick="searchClick"></custom-button>
				<custom-button type="reset" style="margin-right: 10px" @resetClick="resetClick"></custom-button>
				<custom-button type="add" @addClick="addClick" style="margin-right: 10px"></custom-button>
				<custom-button type="add" title="同步" @addClick="synchronousClick"></custom-button>
			</div>
		</navigation-bar>
		<el-row>
			<el-col :span="6">
				<div style="width: 98%">
					<single-table :dataSource="leftDataSource" :tableColumns="leftTableColumns"
						@singleHandleSelectionChange="singleHandleSelectionChange"></single-table>
				</div>
			</el-col>
			<el-col :span="18">
				<table-index :dataSource="dataSource" :tableColumns="tableColumns" :btnList="btnList" :pageSize="pageSize"
					:pageCount="pageCount" :pageNo="pageNo" @pageChange="pageChange" @editRow="editRow" @viewRow="viewRow"
					@deleteRow="deleteRow"></table-index>
			</el-col>
		</el-row>
		<div v-if="dialogShow">
			<custom-dialog :title="dialogTitle" size="large" @dialogClose="dialogClose">
				<form-index :formConfig="formConfig" @formSelectChange="formSelectChange" @submit="submit" ref="form"
					:type="type">
					<!--  <div>这里还可以插入其它内容进行拓展</div> -->
				</form-index>
			</custom-dialog>
		</div>

		<div v-if="periodDialogShow">
			<custom-dialog title="同步周期数据" size="small" @dialogClose="periodDialogClose">
				<period-no-select stage="Ing" cfgType="GroupDrug" @periodSelectClose="periodDialogClose"></period-no-select>
			</custom-dialog>
		</div>
	</div>
</template>

<script>
import { GroupDosingConfigurationFormConfig } from "@/assets/form/managementCenter/testConfiguration/testPeriodConfiguration/groupDosingConfigurationForm"
import customDialog from "@/components/customComponents/customDialog"
import customButton from "@/components/customComponents/customForm/customButton"
import formIndex from "@/components/customComponents/customForm/formIndex"
import singleTable from "@/components/customComponents/customeTable/singleTable"
import tableIndex from "@/components/customComponents/customeTable/tableIndex"
import navigationBar from "@/components/customComponents/managementCenter/navigationBar"
import { AjaxCommon } from "@/libs/apiCommon"
import { PopupCommon } from "@/libs/popupCommon"
import { SessionCommon } from "@/libs/sessionCommon"
import periodNoSelect from "@/components/customComponents/periodNoSelect";
export default {
	components: {
		navigationBar,
		customButton,
		tableIndex,
		singleTable,
		customDialog,
		formIndex,
		periodNoSelect
	},
	data() {
		return {
			periodDialogShow: false,
			groupIdList: [],
			leftSelectIds: [],
			leftDataSource: [],
			leftTableColumns: [
				{
					prop: "name",
					label: "分组名称",
				},
			],
			name: "",
			// table 相关参数  begin
			dataSource: [],
			tableColumns: [
				{
					prop: "periodNo",
					label: "周期号",
				},
				{
					label: "给药信息",
					children: [
						{
							prop: "drugNo",
							label: "给药序号",
						},
						{
							prop: "oppositeDay",
							label: "相对天数",
						},
						{
							prop: "oppositeTime",
							label: "相对时间",
						},
						{
							prop: "drugName",
							label: "药品名称",
						},
						{
							prop: "drugType",
							label: "药品类型",
							dictName: "drug_type",
						},
						{
							prop: "drugWay",
							label: "给药途径",
							dictName: "drug_way",
						},
					],
				},
				{
					label: "其它信息",
					children: [
						{
							prop: "formulation",
							label: "剂型",
							dictName: "drug_form",
						},
						{
							prop: "dosage",
							label: "用量",
						},
						{
							prop: "unit",
							label: "单位",
						},
						{
							prop: "drinkWater",
							label: "饮水",
						},
					],
				},
			],
			btnList: ["edit", "view", "delete"],
			pageSize: 20,
			pageCount: 100,
			pageNo: 1,
			// table 相关参数  end

			// 弹窗是否显示
			dialogShow: false,
			// 弹窗标题 (新增修改不同)
			dialogTitle: "",

			// form的配置信息
			formConfig: "",
			type: "add",
			drugList: [],
			drugTimeList: [],
		}
	},
	methods: {
		periodDialogClose() {
			this.periodDialogShow = false
		},
		synchronousClick() {
			this.periodDialogShow = true
		},
		formSelectChange(e) {
			console.log(e);
			if (e.filedName == 'drugId') {
				let item = this.drugList.find((c) => c.value == e.value)
				const formValue = this.$refs.form.getFormValue()
				formValue.drugName = item.drugName
				formValue.unit = item.drugUnit
				formValue.drugType = item.drugType
				formValue.drugWay = item.drugWay
				formValue.formulation = item.formulation
				this.$refs.form.changeFormValue(formValue)
			}
			if (e.filedName == 'drugNo') {
				let item = this.drugTimeList.find((c) => c.value == e.value)
				const formValue = this.$refs.form.getFormValue()
				formValue.oppositeTime = item.oppositeTime
				formValue.oppositeDay = item.oppositeDay
				this.$refs.form.changeFormValue(formValue)
			}
		},
		submit(d) {
			let self = this
			if (d.id) {
				AjaxCommon("/ccdc/cfg/exp/ing/expIngGroupDrug/edit", "post", d)
					.then((res) => {
						if (res.data.code == 200) {
							PopupCommon.notify("success", "操作成功")
							self.initPage()
							self.dialogClose()
						} else {
							PopupCommon.notify("error", res.data.message)
						}
					})
					.catch((err) => {
						PopupCommon.notify("error", err.message)
					})
			} else {
				d.groupId = this.leftSelectIds[0].id
				console.log(d, "d............")
				delete d.id
				AjaxCommon("/ccdc/cfg/exp/ing/expIngGroupDrug/add", "post", d)
					.then((res) => {
						if (res.data.code == 200) {
							PopupCommon.notify("success", "操作成功")
							self.initPage()
							self.dialogClose()
						} else {
							PopupCommon.notify("error", res.data.message)
						}
					})
					.catch((err) => {
						PopupCommon.notify("error", err.message)
					})
			}
		},
		dialogClose() {
			this.dialogShow = false
		},
		searchClick() {
			this.pageNo = 1
			this.initPage()
		},
		resetClick() {
			this.name = ""
		},
		addClick() {
			if (this.leftSelectIds.length != 1) {
				PopupCommon.notify("info", "请勾选一个分组")
				return
			}
			let formConfigItem = this.formConfig
			formConfigItem.map((t) => {
				Object.keys(t).map((res) => {
					if (res == "fieldName") {
						t.filedValue = ""
					}
				})
			})
			formConfigItem.groupId = this.leftSelectIds[0].id
			this.formConfig = formConfigItem
			this.type = "add"
			this.dialogShow = true
			this.dialogTitle =
				"新增" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
		},
		// 分页点击事件
		pageChange(p) {
			this.pageNo = p
			this.initPage()
		},
		editRow(d) {
			this.type = "edit"
			let formConfigItem = this.formConfig
			formConfigItem.map((t) => {
				Object.keys(t).map((res) => {
					if (res == "fieldName") {
						t.filedValue = d.data[t[res]]
					}
				})
			})
			this.formConfig = formConfigItem
			this.dialogShow = true
			this.dialogTitle =
				"编辑" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
		},
		viewRow(d) {
			this.type = "look"
			let formConfigItem = this.formConfig
			formConfigItem.map((t) => {
				Object.keys(t).map((res) => {
					if (res == "fieldName") {
						t.filedValue = d.data[t[res]]
					}
				})
			})
			this.formConfig = formConfigItem
			this.dialogShow = true
			this.dialogTitle =
				"查看" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
		},
		deleteRow(d) {
			const id = d.data.id
			let self = this
			AjaxCommon(
				"/ccdc/cfg/exp/ing/expIngGroupDrug/delete?id=" + id,
				"delete", { optReason: d.optReason }
			).then((res) => {
				console.log(res, "res...............")
				if (res.data.code == 200) {
					PopupCommon.notify("success", "删除成功")
					self.initPage()
					self.dialogClose()
				} else {
					PopupCommon.notify("error", res.data.message)
				}
			})
		},
		// 左侧选中事件
		singleHandleSelectionChange(d) {
			console.log(d)
			this.leftSelectIds = d
			this.groupIdList = d ? d.map((t) => t.id) : []
			this.initPage()
		},
		// 初始化事件
		initPage() {
			let self = this
			AjaxCommon("/ccdc/cfg/exp/ing/expIngGroupDrug/listByPage", "post", {
				name: this.name,
				groupIdList: this.groupIdList,
				pageNo: this.pageNo,
				pageSize: this.pageSize,
			}).then((res) => {
				if (res.data.code == 200) {
					self.dataSource = res.data.result.records
					self.pageCount = res.data.result.total
				} else {
					PopupCommon.notify("error", res.data.message)
				}
			})
		},
		// 初始化左侧分组数据
		initLeftData() {
			// 获取左侧列表数据  /ccdc/cfg/exp/root/expGroup/listByPage
			AjaxCommon("/ccdc/cfg/exp/root/expGroup/listByPage", "post", {}).then(
				(res) => {
					if (res.data.code == 200) {
						this.leftDataSource = res.data.result.records
					} else {
						this.leftDataSource = []
						PopupCommon.notify("error", res.data.message)
					}
				}
			)
		},
		// 初始化事件
		initSelect() {
			var _this = this;
			AjaxCommon("/ccdc/businessSelect/drugTime", "post", {}).then((res) => {
				if (res.data.code == 200) {
					_this.drugTimeList = res.data.result;
					//_this.formConfig=GroupDosingConfigurationFormConfig(res.data.result)
					AjaxCommon("/ccdc/businessSelect/expUseDrug", "post", {}).then((res1) => {
						if (res1.data.code == 200) {
							_this.drugList = res1.data.result;
							var item = {};
							item.drugList = _this.drugList;
							item.drugTimeList = _this.drugTimeList;
							_this.formConfig = GroupDosingConfigurationFormConfig(item)
						} else {
							PopupCommon.notify("error", res.data.message)
						}
					});
				} else {
					PopupCommon.notify("error", res.data.message)
				}
			});
		},
	},
	mounted() {
		this.initSelect()
		this.initLeftData()
		this.initPage()
	},
}
</script>

<style></style>
